package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//部门控制层
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    //获取所有部门
    @GetMapping
    public Result list() {
        //调用业务
        List<Dept> depts = deptService.list();
        //返回结果
        return Result.success(depts);
    }

    //删除部门
    @LogOperation
    @DeleteMapping
    public Result delete(Integer id) {
        Boolean flag = deptService.deleteById(id);
        if (flag) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    //添加部门
    @LogOperation
    @PostMapping
    public Result save(@RequestBody Dept dept) {

        Boolean falg = deptService.save(dept);
        if (falg) {
            return Result.success();
        }
        return Result.error("添加失败");
    }

    //根据ID查询部门
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        System.out.println("根据ID查询, id=" + id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    //修改部门
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success();
    }
}

